package util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class TagHelper {
	public static Document help(Document doc)
	{
		try {
        	String message = doc.select("#fullQuestionBody").select("p").text().replaceAll("\\<.*?\\>", "");			
			
        	URL url = new URL("http://o2tv.vn/tag.php");
			
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(500000);
			connection.setConnectTimeout(1000000);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write("str="+message.replaceAll("[\\W]+", " "));
            writer.close();
            
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	           	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	           	 String content2 = "";
	           	 while(true)
    			 {
    				 String str = reader.readLine();
    				 if(str==null) break;
    				 content2+=str;   				
    			 }
	           	 doc = Jsoup.parse(content2);	           	 
            }        
            return doc;
        } catch (Exception e) {
			return doc;
		}
		
	}
}
